<?php
if(!isset($_SESSION['subtotal'])) $_SESSION['subtotal'] = 0;
if($_REQUEST['ajax'] == 'true'){
	session_start();
	include "../../connections/con_gl.php";
	
	switch($_REQUEST['act']){
		case 'add' :
		
			$_SESSION['kendaraan'][] 	= $_POST['kendaraan'];
			$_SESSION['txtkendaraan'][] = $_POST['txtkendaraan'];
			$_SESSION['hargasatuan'][] 	= $_POST['hargasatuan'];
			$_SESSION['diskon'][]		= $_POST['diskon'];
			$_SESSION['biaya'][]		= $_POST['biaya'];
			$_SESSION['total'][]		= $_POST['total'];
			$_SESSION['transaksi'][]	= $_POST['transaksi'];
			
			$_SESSION['subtotal'] = 0;
			$_SESSION['tbiaya'] = 0;
			
			foreach($_SESSION['total'] as $total){
				$_SESSION['subtotal'] += $total;
			}
			foreach($_SESSION['biaya'] as $biaya){
				$_SESSION['tbiaya'] += $biaya;
			}
			
			
			$_SESSION['pajak']			= $_SESSION['subtotal']*($_POST['pajak']/100);
			$_SESSION['pph']			= $_SESSION['subtotal']*($_POST['pph']/100);
			$_SESSION['grandtotal']		= $_SESSION['subtotal']+$_SESSION['tbiaya']+$_SESSION['pajak']- $_SESSION['pph'];
			break;
		
		case 'edit' :
				
			$pos = $_POST['pos'];
			
			$_SESSION['kendaraan'][$pos] 	= $_POST['kendaraan'];
			$_SESSION['txtkendaraan'][$pos] = $_POST['txtkendaraan'];
			$_SESSION['hargasatuan'][$pos] 	= $_POST['hargasatuan'];
			$_SESSION['diskon'][$pos]		= $_POST['diskon'];
			$_SESSION['biaya'][$pos]		= $_POST['biaya'];
			$_SESSION['total'][$pos]		= $_POST['total'];
			$_SESSION['transaksi'][$pos]	= $_POST['transaksi'];
			
			$_SESSION['subtotal'] = 0;
			$_SESSION['tbiaya'] = 0;
			foreach($_SESSION['total'] as $total){
				$_SESSION['subtotal'] += $total;
			}
			
			foreach($_SESSION['biaya'] as $biaya){
				$_SESSION['tbiaya'] += $biaya;
			}
			
			$_SESSION['pajak']			= $_SESSION['subtotal']*($_POST['pajak']/100);
			$_SESSION['pph']			= $_SESSION['subtotal']*($_POST['pph']/100);
			$_SESSION['grandtotal']		= $_SESSION['subtotal']+$_SESSION['tbiaya']+$_SESSION['pajak']- $_SESSION['pph'];
			break;
 		
		case 'delete' :
			$pos = $_POST['pos'];
			
			$_SESSION['subtotal'] -= $_SESSION['total'][$pos];
			$_SESSION['tbiaya'] -= $_SESSION['biaya'][$pos];
			$_SESSION['pajak']			= $_SESSION['subtotal']*($_POST['pajak']/100);
			$_SESSION['pph']			= $_SESSION['subtotal']*($_POST['pph']/100);
			$_SESSION['tbiaya']			= $_SESSION['tbiaya'];
			$_SESSION['grandtotal']		= $_SESSION['subtotal']+$_SESSION['tbiaya']+$_SESSION['pajak']- $_SESSION['pph'];
			
			unset($_SESSION['kendaraan'][$pos]);
			unset($_SESSION['txtkendaraan'][$pos]);
			unset($_SESSION['hargasatuan'][$pos]);
			unset($_SESSION['diskon'][$pos]);
			unset($_SESSION['biaya'][$pos]);
			unset($_SESSION['total'][$pos]);
			unset($_SESSION['transaksi'][$pos]);
		
			
			$_SESSION['kendaraan']		=  array_values($_SESSION['kendaraan']);
			$_SESSION['txtkendaraan']	=  array_values($_SESSION['txtkendaraan']);
			$_SESSION['hargasatuan']	=  array_values($_SESSION['hargasatuan']);
			$_SESSION['diskon']			=  array_values($_SESSION['diskon']);
			$_SESSION['biaya']			=  array_values($_SESSION['biaya']);
			$_SESSION['total']			=  array_values($_SESSION['total']);
			$_SESSION['transaksi']		=  array_values($_SESSION['transaksi']);
			break;
	}
}
?>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="datatable">
<tr>
		<th width="15%"><strong>Pilih Kendaraan</strong></th>
    <th width="15%" align="center"><strong>Harga </strong></th>
		<th width="5%" align="center"><strong>Diskon</strong></th>
		<th width="15%" align="center">Biaya Lainnya </th>
		<th width="15%" align="center"><strong>Total</strong></th>
		<th align="center"><strong>Info</strong></th>
		<th width="6%" align="center"><strong>Aksi</strong></th>
	</tr>
	<?php
	if(isset($_SESSION['kendaraan'])){
		$i=0;
		foreach($_SESSION['kendaraan'] as $idk){
			if($_POST['status']=='edit' && $_POST['pos']==$i){
			?>
			<tr valign="top" bgcolor="#FFFF00">
				<td align="left" style="background:none;border:none;"> 
					<!-- Added by suwondo -->
					<input type="hidden" name="kendaraan" id="kendaraan" value="<?php echo $_SESSION['kendaraan'][$i]; ?>" />
					<input type="text" name="txtkendaraan" id="txtkendaraan" value="<?php echo $_SESSION['txtkendaraan'][$i]; ?>" placeholder="Cari Kendaraan ..." size="30" readonly style="background-color:#fff;border:1px solid #999; height:16px;" onclick="NewWindow('index.php?component=kendaraan&amp;task=list&amp;barang='+document.getElementById('gudang').value+'&open=window','name','1200','600','yes');return false" /></td>
				<td align="right" style="background:none;border:none;"><label id="divHargaKendaraan">              
					Rp. <input name="hargasatuan" type="text" id="hargasatuan" value="<?php echo $_SESSION['hargasatuan'][$i]; ?>"onkeyup="jaavascript:if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value); var harga=parseInt(this.value); document.getElementById('total').value=parseInt(document.getElementById('hargasatuan').value) - (parseInt(document.getElementById('hargasatuan').value) * parseInt(document.getElementById('diskon').value) / 100);
            " size="10" maxlength="10" autocomplete="off" />              
					</label>				</td>
				<td align="center" style="background:none;border:none;">
				<label><input name="diskon" type="text" id="diskon" value="<?php echo $_SESSION['diskon'][$i]; ?>" onkeyup="avascript:if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value); 
            document.getElementById('total').value=parseInt(document.getElementById('hargasatuan').value) - (parseInt(document.getElementById('hargasatuan').value) * parseInt(document.getElementById('diskon').value) / 100);" value="0" size="2" maxlength="3" autocomplete="off" /></label></td>
				<td align="right" style="background:none;border:none;"><label>Rp.              
		      <input name="biaya" type="text" id="biaya" value="<?php echo $_SESSION['biaya'][$i]; ?>" onkeyup="avascript:if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value); 
           document.getElementById('total').value=parseInt(document.getElementById('hargasatuan').value) - (parseInt(document.getElementById('hargasatuan').value) * parseInt(document.getElementById('diskon').value) / 100);" size="10" maxlength="10" autocomplete="off" /></label></td>
				<td align="right" style="background:none;border:none;">
					<label>Rp. <input name="total" type="text" id="total" value="<?php echo $_SESSION['total'][$i]; ?>" size="10" maxlength="15" /></label>				</td>
				<td align="center" style="background:none;border:none;">
					<label><textarea name="transaksi" cols="40" rows="2" id="transaksi"><?php echo $_SESSION['transaksi'][$i]; ?></textarea></label>				</td>
				<td align="center" style="background:none;border:none;">
					<input name="edit" type="button" id="edit" style="background-image:url(images/quotes.png);" value="&nbsp;" onclick="var err=''; 
					if(document.getElementById('kendaraan').value=='') err+='kendaraan harus dipilih\n'; 
					if(document.getElementById('txtkendaraan').value=='') err+='kendaraan harus dipilih\n'; 
                    
					if(document.getElementById('hargasatuan').value=='') err+='Harga harus diisi\n'; 
					if(err!='') alert(err);
					else kirimRequest('components/kontrak/detail.php', 'ajax=true&act=edit&pos=<?php echo $i; ?>&kendaraan='+document.getElementById('kendaraan').value+'&txtkendaraan='+document.getElementById('txtkendaraan').value+'&hargasatuan='+document.getElementById('hargasatuan').value+'&diskon='+document.getElementById('diskon').value+'&diskon='+document.getElementById('diskon').value+'&biaya='+document.getElementById('biaya').value+'&total='+document.getElementById('total').value+'&transaksi='+document.getElementById('transaksi').value+'&biayalain='+document.getElementById('biayalain').value+'&pajak='+document.getElementById('pajak').value+'&pph='+document.getElementById('pph').value, 'ajaxDiv', 'div');" class="checkbutton" title="Edit Transaksi"/>				</td>
			</tr>
			<?php
			}else{
				echo "<tr>";
					echo "<td>".($_SESSION['txtkendaraan'][$i])."</td>";
					echo "<td align='right'>Rp. ".number_format($_SESSION['hargasatuan'][$i],0,',','.')."</td>";
					echo "<td align='center'>".($_SESSION['diskon'][$i])."%</td>";		
					echo "<td align='center'>Rp. ".number_format($_SESSION['biaya'][$i],0,',','.')."</td>";			
					echo "<td align='right'>Rp. ".number_format($_SESSION['total'][$i],0,',','.')."</td>";
					echo "<td>".($_SESSION['transaksi'][$i])."</td>";
					echo "<td align='center'>";
					echo "<a style='cursor:pointer;' onclick=\"javascript: kirimRequest('components/kontrak/detail.php', 'ajax=true&status=edit&pos=$i&biayalain='+document.getElementById('biayalain').value+'&biaya='+document.getElementById('biaya').value+'&pph='+document.getElementById('pph').value+'&pajak='+document.getElementById('pajak').value, 'ajaxDiv', 'div');\" title='Edit data'><img src='images/edit.png' /></a>&nbsp;&nbsp;";
					echo "<a style='cursor:pointer;' onclick=\"javascript: kirimRequest('components/kontrak/detail.php', 'ajax=true&act=delete&pos=$i&biayalain='+document.getElementById('biayalain').value+'&biaya='+document.getElementById('biaya').value+'&pph='+document.getElementById('pph').value+'&pajak='+document.getElementById('pajak').value, 'ajaxDiv', 'div');\" title='Hapus data'><img src='images/delete.png' /></a>";
					echo "</td>";
				echo "</tr>";
			}
			$i++;
		}
	}
	?>	
    <tr valign="top" bgcolor="#FFFFFF">
        <td align="left" style="background:none;border:none;"> 
			<!-- Added by suwondo -->
			<input type="hidden" name="kendaraan" id="kendaraan" />
			<input type="text" name="txtkendaraan" id="txtkendaraan" placeholder="Cari Kendaraan ..." size="30" readonly style="background-color:#fff;border:1px solid #999; height:16px;" onclick="NewWindow('index.php?component=kendaraan&amp;task=list&amp;barang=false&amp;area=<?php echo $_SESSION['area'];?>	&amp;open=window','name','1200','600','yes');return false" />		</td>
        <td align="right" style="background:none;border:none;"><label id="divHargaKendaraan">              
            Rp. <input name="hargasatuan" type="text" id="hargasatuan" onkeyup="javascript:if(this.value=='' || isNaN(this.value)) this.value='0'; 
            this.value=parseInt(this.value);               
            this.form.total.value= parseInt(this.form.hargasatuan.value) - (parseInt(this.form.hargasatuan.value) * parseInt(this.form.diskon.value) / 100);" size="10" maxlength="10" autocomplete="off" /> 
            %              
            </label>		</td>
        <td align="center" style="background:none;border:none;"><label>
        <input name="diskon" type="text" id="diskon" onkeyup="javascript:if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value);this.form.total.value= parseInt(this.form.hargasatuan.value) - (parseInt(this.form.hargasatuan.value) * parseInt(this.form.diskon.value) / 100);" value="0" size="2" maxlength="3" autocomplete="off" />
              </label></td>
		<td align="right" style="background:none;border:none;"><label>Rp.
      <input name="biaya" type="text" id="biaya" onkeyup="javascript:if(this.value=='' || isNaN(this.value)) this.value='0'; this.value=parseInt(this.value); this.form.total.value= parseInt(this.form.hargasatuan.value) - (parseInt(this.form.hargasatuan.value) * parseInt(this.form.diskon.value) / 100);" value="0" size="10" maxlength="10" autocomplete="off" /></label></td>
		<td align="right" style="background:none;border:none;">
			<label>Rp. <input name="total" type="text" id="total" size="10" maxlength="15" /></label>		</td>
		<td align="center" style="background:none;border:none;">
			<label><textarea name="transaksi" cols="40" rows="2" id="transaksi"></textarea></label>		</td>
		<td align="center" style="background:none;border:none;">
			<input name="add" type="button" id="add" value="&nbsp;" onclick="var err=''; 
			if(document.getElementById('kendaraan').value=='') err+='kendaraan harus dipilih\n'; 
			if(document.getElementById('txtkendaraan').value=='') err+='kendaraan harus dipilih\n'; 
			if(document.getElementById('hargasatuan').value=='') err+='Harga harus diisi\n'; 
			if(err!='') alert(err);
			else kirimRequest('components/kontrak/detail.php', 'ajax=true&act=add&kendaraan='+document.getElementById('kendaraan').value+'&txtkendaraan='+document.getElementById('txtkendaraan').value+'&hargasatuan='+document.getElementById('hargasatuan').value+'&diskon='+document.getElementById('diskon').value+'&biaya='+document.getElementById('biaya').value+'&total='+document.getElementById('total').value+'&transaksi='+document.getElementById('transaksi').value+'&biayalain='+document.getElementById('biayalain').value+'&pph='+document.getElementById('pph').value+'&pajak='+document.getElementById('pajak').value, 'ajaxDiv', 'div');" class="checkbutton" title="Tambah Transaksi"/>		</td>
	</tr>
	<tr>
		<td width="55%" align="right" colspan="3">&nbsp;</td>
		<td width="12%" align="right"><strong>Subtotal</strong></td>
		<td width="12%" align="right"><strong>Rp. <label id="subtotal"><?php if($_SESSION['subtotal']=='') echo "0"; else echo number_format($_SESSION['subtotal'],0,',','.'); ?></label></strong></td>
		<td align="center"><strong>&nbsp;</strong></td>
		<td width="6%" align="center"><strong>&nbsp;</strong></td>
	</tr>
	<tr>
		<td width="55%" align="right" colspan="3">&nbsp;</td>
		<td width="12%" align="right"><strong>Biaya Lain</strong></td>
		<td width="12%" align="right">Rp. <input type="text" name="biayalain" id="biayalain" size="10" value="<?php if($_SESSION['tbiaya']=='') echo "0"; else echo number_format($_SESSION['tbiaya'],0,',','.'); ?>" autocomplete="off" /></td>
		<td align="center"><strong>&nbsp;</strong></td>
		<td width="6%" align="center"><strong>&nbsp;</strong></td>
	</tr>
	<tr>
	  <td colspan="3" align="center">&nbsp;</td>
	  <td align="right"><strong>PPN</strong>&nbsp;&nbsp;
        <input type="text" name="pajak" id="pajak" size="3" value="<?php if(isset($_POST['pajak'])) echo $_POST['pajak']; else echo "10"; ?>" style="text-align:right;" onkeyup="javascript: if(this.value=='' || isNaN(this.value)) this.value='10'; this.value=parseInt(this.value); pajakval=<?php echo $_SESSION['subtotal']; ?>*parseInt(this.value)/100; document.getElementById('pajakval').innerHTML=number_format(pajakval,0,',','.'); grandtotal=<?php echo $_SESSION['subtotal']; ?>+parseInt(document.getElementById('biayalain').value)+(<?php echo $_SESSION['subtotal']; ?>*parseInt(this.value)/100)-(<?php echo $_SESSION['subtotal']; ?>*parseInt(document.getElementById('pph').value)/100); document.getElementById('grandtotal').innerHTML=number_format(grandtotal,0,',','.');" autocomplete="off" />
&nbsp;%</td>
	  <td align="right"><strong>Rp.
          <label id="pajakval"><?php echo number_format($_SESSION['pajak'],0,',','.'); ?></label>
	  </strong></td>
	  <td align="center">&nbsp;</td>
	  <td align="center">&nbsp;</td>
  </tr>
	<tr>
		<td width="55%" align="right" colspan="3">&nbsp;</td>
		<td width="12%" align="right"><strong>PPH 23 </strong>&nbsp;&nbsp;
          <input type="text" name="pph" id="pph" size="3"  value="<?php if(isset($_POST['pph'])) echo $_POST['pph']; else echo "2"; ?>"  style="text-align:right;" onkeyup="javascript: if(this.value=='' || isNaN(this.value)) this.value='2'; this.value=parseInt(this.value); pphval=<?php echo $_SESSION['subtotal']; ?>*parseInt(this.value)/100; document.getElementById('pphval').innerHTML=number_format(pphval,0,',','.'); grandtotal=<?php echo $_SESSION['subtotal']; ?>+parseInt(document.getElementById('biayalain').value)+(<?php echo $_SESSION['subtotal']; ?>*parseInt(document.getElementById('pajak').value)/100)-(<?php echo $_SESSION['subtotal']; ?>*parseInt(this.value)/100); document.getElementById('grandtotal').innerHTML=number_format(grandtotal,0,',','.');" autocomplete="off"/>
&nbsp;&nbsp;%</td>
		<td width="12%" align="right"><strong>Rp.
            <label id="pphval"><?php echo number_format($_SESSION['pph'],0,',','.'); ?></label>
        <label id="pphval"></label></strong></td>
		<td align="center"><strong>&nbsp;</strong></td>
		<td width="6%" align="center"><strong>&nbsp;</strong></td>
	</tr>
	<tr>
		<td width="55%" align="right" colspan="3">&nbsp;</td>
		<td width="12%" align="right"><strong>Grand Total</strong></td>
		<td width="12%" align="right"><strong>Rp. <label id="grandtotal"><?php echo number_format($_SESSION['grandtotal'],0,',','.'); ?></label></strong></td>
		<td align="center"><strong>&nbsp;</strong></td>
		<td width="6%" align="center"><strong>&nbsp;</strong></td>
	</tr>
</table>
